<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/9/11
 * Time: 10:24
 */

namespace Admin\Controller;


use Admin\Model\ClassTaskModel;

class CoursesController extends CommonController
{
    public function index(){
        $map = array();
        if (I('get.keyword') != '') {
            $map['name'] = array('like', '%' . I('get.keyword') . '%');
        }
        $list = D('courses')->getlist($map);

        if ($list){
            foreach ($list['list'] as $key=>$val){
                $list['list'][$key]['value'] = substr_replace(strip_tags(html_entity_decode($val['value'])),"......",50);
                $list['list'][$key]['type'] = D('class')->where(array('id'=>$val['type']))->getField('name');
            }
        }

        $this->assign('count', $list['count']);
        $this->assign('totalPages', $list['totalPages']);// 总分页数
        $this->assign('nowPage', $list['nowPage']);// 当前页
        $this->assign('list', $list['list']);
        $this->assign('page',$list['show']);
        $this->assign('discount',M('discount')->find(1));//打折价格
        $this->display();
    }

    /**
     * 上课周列表
     */
    public function setClass(){
        $courseId = I('get.id');
        $classes = M('class_task')->where(array('course_id'=>$courseId))->select();
        if ($classes){
            foreach ($classes as $key=>$class){
                $classes[$key]['name'] = M('courses')->where(array('id'=>$class['course_id']))->getField('name');
            }
        }
        $this->assign('lists',$classes);
        $this->display();
    }

    /**
     * 上课周时间修改
     */
    public function class_edit(){
        $id = I('post.id');
        $date = I('post.data');
        M('class_task')->save(array('id'=>$id,'class_date'=>$date));
    }

    public function edit()
    {
        if (IS_POST) {
            $msg = array('errno' => 1, 'error' => '编辑失败，请重新编辑', 'url' => '');
            $data = I('post.');
            $action = I('action', '');

            if ($action == 'add') {
                unset($data['action']);
                unset($data['id']);
                $nameInfo = D('courses')->getOne(array('name'=>$data['name']));//查看课程是否存在
                if ($nameInfo) {
                    $msg ['errno'] = 1;
                    $msg ['error'] = '课程已存在';
                    $this->ajaxReturn($msg);
                }
                $data['createtime'] = date("Y-m-d H:i:s",time());//添加时间
                $id = D('courses')->insert($data);//添加数据
                if ($id) {
                    ClassTaskModel::setTask($id);
                    $msg ['errno'] = 0;
                    $msg ['error'] = '添加成功！';
                    $msg ['url'] = U('index');
                }
            } else if ($action == 'edit') {
                $data['id'] = I('id');
                $nameInfo = D('courses')->getOne(array('name'=>$data['name'],'id'=>array('neq',$data['id'])));//查看课程是否存在
                if ($nameInfo) {
                    $msg ['errno'] = 1;
                    $msg ['error'] = '该课程已存在';
                    $this->ajaxReturn($msg);
                }
                unset($data['action']);
                D('courses')->edit($data);//更新数据
                $msg ['errno'] = 0;
                $msg ['error'] = '编辑成功！';
                $msg ['url'] = U('index');
            } else {
                $msg ['errno'] = 1;
                $msg ['error'] = '数据异常';
                $this->ajaxReturn($msg);
            }
            $this->ajaxReturn($msg);
        } else {
            $action = I('action', '');
            if ($action == 'edit') {
                $data['id'] = I('id');
                $info = D('courses')->getOne($data);//获取直营产品
                $this->assign('info', $info);
            }
            $this->assign('types',D('class')->select());
            $this->display();
        }
    }

    /**
     * 价格打折设置
     */
    public function setDis(){
        $dis = I('post.data');
        $result = M('discount')->save(array('id'=>1,'discount'=>$dis));
        if ($result !==false){
            $this->ajaxReturn(1);
        }
        $this->error('设置失败');
    }

    public function tasks(){
        $map = array();
        if (I('get.keyword') != '') {
            $map['name'] = array('like', '%' . I('get.keyword') . '%');
        }
        $list = D('ClassSignup')->getlist($map);

        if ($list){
            foreach ($list['list'] as $key=>$val){
                $list['list'][$key]['userName'] = D('wx_users')->where(array('id'=>$val['user_id']))->getField('surname').D('wx_users')->where(array('id'=>$val['user_id']))->getField('name');
                $courseId = D('class_task')->where(array('id'=>$val['class_id']))->getField('course_id');
                $list['list'][$key]['courseName'] = D("courses")->where(array('id'=>$courseId))->getField('name');
                $list['list'][$key]['createtime'] = date("Y-m-d H:i:s",$val['createtime']);
                $list['list'][$key]['classSort'] = D('class_task')->where(array('id'=>$val['class_id']))->getField('sort');
                if ($val['is_sign'] == '1'){
                    $list['list'][$key]['status'] = '签到';
                }
                if ($val['is_leave'] == '1'){
                    $list['list'][$key]['status'] = '请假';
                }
            }
        }

        $this->assign('count', $list['count']);
        $this->assign('totalPages', $list['totalPages']);// 总分页数
        $this->assign('nowPage', $list['nowPage']);// 当前页
        $this->assign('list', $list['list']);
        $this->assign('page',$list['show']);
        $this->display('Tasks/index');
    }
    public function del(){
        $ids = I('ids');
        $msg ['errno'] = 0;
        $msg ['error'] = "删除成功";
        if (is_array($ids)) {
            foreach ($ids as $id) {
                $data['id'] = $id;
                $flag = D('courses')->del($data);
            }
        }
        if (!$flag) {
            $msg ['errno'] = 1;
            $msg ['error'] = "删除失败！";
        }

        $this->ajaxReturn($msg);
    }
}